package com.css.zfzx.data.primary.fxpicture.service.impl;

import ch.ethz.ssh2.Connection;
import com.css.zfzx.data.common.Constant;
import com.css.zfzx.data.common.Enum;
import com.css.zfzx.data.common.ProvinceCityAreaUtil;
import com.css.zfzx.data.common.RemoteCommandUtil;
import com.css.zfzx.data.primary.fxpicture.repository.FxPictureNativeRepository;
import com.css.zfzx.data.primary.fxpicture.repository.entity.FxPictureEntity;
import com.css.zfzx.data.primary.fxpicture.service.FxPictureService;
import com.css.zfzx.data.primary.sysAttachmentInfo.repository.SysAttachmentInfoRepository;
import com.css.zfzx.data.primary.sysAttachmentInfo.repository.entity.SysAttachmentInfoEntity;
import com.css.zfzx.data.thirdary.fjgReconciliation.repository.FjgReconciliationNativeRepository;
import com.css.zfzx.data.thirdary.fjgReconciliation.repository.FjgReconciliationRepository;
import com.css.zfzx.data.thirdary.fjgReconciliation.repository.entity.SjtgfFjghshDzSjdzbEntity;
import com.css.zfzx.data.thirdary.ftpReconciliation.repository.FtpReconciliationRepository;
import com.css.zfzx.data.thirdary.ftpReconciliation.repository.entity.SjtgfFjghshDzSgysjbEntity;
import com.css.zfzx.data.thirdary.jgReconciliation.service.ReconciliationService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Service
@Slf4j
@Data
public class FxPictureServiceImpl implements FxPictureService {

    /**
     * linux地址
     **/
    //@Value("${linux.linux-ip}")
    public String linuxAddress = "10.7.208.209";
    /**
     * linux用户名
     **/
    //@Value("${linux.linux-user}")
    private String linuxUsername = "lhl";
    /**
     * linux密码
     **/
    //@Value("${linux.linux-pwd}")
    private String linuxPassword = "107208209";

    private final FxPictureNativeRepository fxPictureNativeRepository;
    private final ReconciliationService reconciliationService;
    private final SysAttachmentInfoRepository sysAttachmentInfoRepository;
    private final FtpReconciliationRepository ftpReconciliationRepository;
    private final FjgReconciliationRepository fjgReconciliationRepository;
    private final FjgReconciliationNativeRepository fjgReconciliationNativeRepository;

    @Autowired
    public FxPictureServiceImpl(FxPictureNativeRepository fxPictureNativeRepository, ReconciliationService reconciliationService, SysAttachmentInfoRepository sysAttachmentInfoRepository, FtpReconciliationRepository ftpReconciliationRepository, FjgReconciliationRepository fjgReconciliationRepository, FjgReconciliationNativeRepository fjgReconciliationNativeRepository) {
        this.fxPictureNativeRepository = fxPictureNativeRepository;
        this.reconciliationService = reconciliationService;
        this.sysAttachmentInfoRepository = sysAttachmentInfoRepository;
        this.ftpReconciliationRepository = ftpReconciliationRepository;
        this.fjgReconciliationRepository = fjgReconciliationRepository;
        this.fjgReconciliationNativeRepository = fjgReconciliationNativeRepository;
    }

    // @Override
    // public void findData(int flag) {
    //     List<FxPictureEntity> fxPictureEntities =null;
    //     if (flag==1) {//查询新增数据
    //         LocalDateTime createTime = ftpReconciliationRepository.findCreateTime(Constant.SJDXXHMC_R);
    //         fxPictureEntities = fxPictureNativeRepository.findFxPictureCreate(createTime);
    //     }else if(flag==2){//查询修改数据
    //         LocalDateTime updateTime = ftpReconciliationRepository.findUpdateTime(Constant.SJDXXHMC_R);
    //         fxPictureEntities = fxPictureNativeRepository.findFxPictureUpdate(updateTime);
    //     }
    //     if (fxPictureEntities.size() == 0) {
    //         if (flag==1) {//查询新增数据
    //             System.out.println("fx_picture:时间" + new Date() + ",添加查询了数据：" + fxPictureEntities.size() + " 条");
    //         }else if(flag==2){//查询修改数据
    //             System.out.println("fx_picture:时间" + new Date() + ",修改查询了数据：" + fxPictureEntities.size() + " 条");
    //         }
    //          return;
    //     }
    //     String filePath = "";
    //     List<SjtgfFjghshDzSgysjbEntity> saveList = new ArrayList<>();
    //     Connection conn = RemoteCommandUtil.login(Enum.SERVER_1.getAddress(), Enum.SERVER_1.getUsername(), Enum.SERVER_1.getPassword());
    //     Connection conn2 = RemoteCommandUtil.login(Enum.SERVER_2.getAddress(), Enum.SERVER_2.getUsername(),
    //             Enum.SERVER_2.getPassword());
    //     StringBuilder dataIds = new StringBuilder();//文件Id
    //     StringBuilder wjmcs = new StringBuilder();//存储文件名
    //     StringBuilder wjljs = new StringBuilder();//存储文件路径
    //     for (FxPictureEntity entity : fxPictureEntities) {
    //         String code = ProvinceCityAreaUtil.getCode(entity.getProvince(), entity.getCity(), entity.getArea());
    //         if (code == null) {
    //             throw new RuntimeException("province:{}" + entity.getProvince() + "city:{}" + entity.getCity() +
    //                     "area:{}" + entity.getArea() + "的区划编码获取失败，请检查后再导入");
    //         }
    //         StringBuilder wjmc = new StringBuilder();//存储文件名
    //         StringBuilder wjlj = new StringBuilder();//存储文件路径
    //         StringBuilder wjlx = new StringBuilder();//存储文件类型
    //         StringBuilder zykjdx = new StringBuilder();//存储文件大小
    //         String result = "";
    //         List<String> asList = Arrays.asList(entity.getAttachId().split(","));
    //         for (String attachId : asList) {
    //             SysAttachmentInfoEntity sysEntity = sysAttachmentInfoRepository.getByAttachIdAndIsValid(attachId, "1");
    //             if (sysEntity == null) {
    //                 continue;
    //             }
    //             String attachName = sysEntity.getAttachName();
    //             wjmc.append(",").append(attachName);
    //             wjlx.append(",").append(attachName.split("\\.")[attachName.split("\\.").length - 1]);
    //             String attachPath = sysEntity.getAttachPath();
    //             wjlj.append(",").append(attachPath);
    //
    //             /*String cmd = "find /data/fujian/fxpg -name " + attachName;
    //             result = RemoteCommandUtil.execmd(conn, cmd);
    //             if (result.split("\\n").length != 1) {
    //                 result = "/data/fujian/fxpg/" + attachName;
    //             }
    //             String cmd = "du -b " + result.substring(0, result.indexOf("\n"));*/
    //             result = "/data/fujian/SHP/" + attachName;
    //             String cmd = "du -b " + result;
    //             result = RemoteCommandUtil.execmd(conn, cmd);
    //             double total = Double.parseDouble(result.substring(0, result.indexOf("/")));
    //             total = total / 1024 / 1024;
    //             if (total > 1) {
    //                 total = total / 1024;
    //             }
    //             zykjdx.append(",").append(total);//得到文件占用内存的大小
    //             if ("地震灾害房屋直接经济损失评估图".equals(entity.getType())) {
    //                 filePath = Constant.FTP_PATH1;
    //             } else if ("地震灾害房屋破坏导致的死亡人口评估图".equals(entity.getType())) {
    //                 filePath = Constant.FTP_PATH2;
    //             } else {
    //                 filePath = Constant.FTP_PATH3;
    //             }
    //             filePath = filePath + code;
    //             String cmd2 = "mkdir -p " + filePath;
    //             RemoteCommandUtil.execmd(conn2, cmd2);
    //             cmd = "scp " + result + " " + Enum.SERVER_2.getUsername() + "@" + Enum.SERVER_2.getAddress() + ":"
    //                     + filePath + code + "/";
    //             RemoteCommandUtil.execmd(conn, cmd);
    //         }
    //         SjtgfFjghshDzSgysjbEntity sjtgfFjghshDzSgysjbEntity = new SjtgfFjghshDzSgysjbEntity();
    //         dataIds.append(",").append(entity.getId());
    //         sjtgfFjghshDzSgysjbEntity.setDataid(entity.getId());
    //         wjmcs.append(",").append(wjmc.substring(1));
    //         sjtgfFjghshDzSgysjbEntity.setWjmc(wjmc.substring(1));
    //         wjljs.append(",").append(wjlj.substring(1));
    //         sjtgfFjghshDzSgysjbEntity.setWjlj(wjlj.substring(1));
    //         sjtgfFjghshDzSgysjbEntity.setWjlx(wjlx.substring(1));
    //         sjtgfFjghshDzSgysjbEntity.setCreatetime(entity.getCreateTime());
    //         sjtgfFjghshDzSgysjbEntity.setUpdatetime(ftpReconciliationRepository.getTimeNow());
    //         if (Constant.SJDXXHMC_E.equals(entity.getType())) {
    //             sjtgfFjghshDzSgysjbEntity.setSjdxxhmc(Constant.SJDXXHMC_E);
    //         } else if (Constant.SJDXXHMC_R.equals(entity.getType())) {
    //             sjtgfFjghshDzSgysjbEntity.setSjdxxhmc(Constant.SJDXXHMC_R);
    //         }
    //         sjtgfFjghshDzSgysjbEntity.setCglx(Constant.CGLX_TJ);
    //         sjtgfFjghshDzSgysjbEntity.setSjl(asList.size() + "");
    //         sjtgfFjghshDzSgysjbEntity.setSjzysm(entity.getPictureDescription());
    //         sjtgfFjghshDzSgysjbEntity.setZykjdx(zykjdx.substring(1));
    //         sjtgfFjghshDzSgysjbEntity.setSshy(Constant.SSHY_DZ);
    //         //根据名称获取行政编码
    //         if (!StringUtils.isEmpty(entity.getProvince()) && StringUtils.isEmpty(entity.getCity())) {
    //             sjtgfFjghshDzSgysjbEntity.setXzqha(code.substring(0, 2) + "0000");
    //         }
    //         if (!StringUtils.isEmpty(entity.getCity()) && StringUtils.isEmpty(entity.getArea())) {
    //             sjtgfFjghshDzSgysjbEntity.setXzqha(code.substring(0, 2) + "0000");
    //             sjtgfFjghshDzSgysjbEntity.setXzqhb(code.substring(0, 4) + "00");
    //         }
    //         if (!StringUtils.isEmpty(entity.getArea())) {
    //             sjtgfFjghshDzSgysjbEntity.setXzqha(code.substring(0, 2) + "0000");
    //             sjtgfFjghshDzSgysjbEntity.setXzqhb(code.substring(0, 4) + "00");
    //             sjtgfFjghshDzSgysjbEntity.setXzqhc(code);
    //         }
    //         sjtgfFjghshDzSgysjbEntity.setSjzt(Constant.SJZT_I);
    //         sjtgfFjghshDzSgysjbEntity.setShlczt(Constant.SHLCZT_ZZSH);
    //         sjtgfFjghshDzSgysjbEntity.setShzt(Constant.SHZT_TG);
    //         saveList.add(sjtgfFjghshDzSgysjbEntity);
    //     }
    //     RemoteCommandUtil.closeConn(conn);
    //     RemoteCommandUtil.closeConn(conn2);
    //     long startIn = System.currentTimeMillis();
    //     ftpReconciliationRepository.saveAll(saveList);
    //     long endIn = System.currentTimeMillis();
    //     System.out.println("fx_picture:插入数据：" + saveList.size() + " 条,用时  " + (endIn - startIn));
    //     SjtgfFjghshDzSjdzbEntity sjtgfFjghshDzSjdzbEntity = new SjtgfFjghshDzSjdzbEntity();
    //     sjtgfFjghshDzSjdzbEntity.setDataid(dataIds.substring(1));
    //     sjtgfFjghshDzSjdzbEntity.setBmc("fx_picture");
    //     sjtgfFjghshDzSjdzbEntity.setLch(1);
    //     sjtgfFjghshDzSjdzbEntity.setWjmc(wjmcs.substring(1));
    //     sjtgfFjghshDzSjdzbEntity.setWjlj(wjljs.substring(1));
    //     sjtgfFjghshDzSjdzbEntity.setSjzw(null);
    //     sjtgfFjghshDzSjdzbEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
    //     fjgReconciliationRepository.save(sjtgfFjghshDzSjdzbEntity);
    // }
}
